import java.util.ArrayList;



public class Generation {
	
	
	//int played[];
	int score[];
	int remainGames[];
	ArrayList<Integer> gameNum;
	
	public Generation(){
		//represent
		score = new int[100];
		remainGames = new int[100];
		gameNum = new ArrayList <Integer>();
		
		//initialize the arrays
		for(int i=0; i<100; i++){
			score[i] = 0;
			remainGames[i] = 10;
			gameNum.add(i, i);
		}
		
		
	}
	
	//the function create a new generation
	void createNewGen(int m[][]){
		
	}
	
	
	void fitness(int m[][]){
		
		int temp1, temp2, tempIndx1, tempIndx2;
		
		//while there are chromosomes that not played 10 games
		while (!gameNum.isEmpty()){
			tempIndx1 = choose random from 0 gameNum.size();
			tempIndx2 = choose random from 0 gameNum.size();
			
			//index in tha array remainGames
			temp1 = gameNum.get(tempIndx1);
			temp2 = gameNum.get(tempIndx2);
			
			// if tempIndx2 is the same index of the chromosome	we try up to 10 times		
			for(int i=0; (i<10)&&(tempIndx1==tempIndx2); i++){
				tempIndx2 choose random from 0 gameNum.size();
			}
			temp2 = gameNum.get(tempIndx2);
			
			//decrease from the remainGames
			remainGames[temp1]--;
			remainGames[temp2]--;
			
			// if temp1 played 10 games remove his value from the list
			if (remainGames[temp1]==0){
				gameNum.remove(tempIndx1);				
			}
			
			//in mostly cases we get that temp1 and temp2 are difrent
			if(temp1!=temp2){
				// if temp2 played 10 games remove his value from the list
				if(remainGames[temp2]==0){
					gameNum.remove(tempIndx2);
				}
			}
	
			// play a game between 2 chromosoms
			play(m, temp1, temp2);
		}
		//end of the while
	}
	//end of fitness
	
	
	
	/*
	 * the function get 2 chromosoms
	 * and compete between them 1000 movements
	 */
	void play(int m[][], int chromo1, int chromo2){
		
		int choice1, choice2;
		int chromo1_his[], chromo2_his[], chrom1[], chrom2[];
		chromo1_his = new int[8];
		chromo2_his = new int[8];
		chrom1 = new int[264];
		chrom2 = new int[264];
		
		//copy chromosom for easy use
		for(int i=0; i<264; i++){
			chrom1[i] = m[chromo1][i];
			chrom2[i] = m[chromo2][i];
		}				
		
		//the first history - fictitious history
		for(int i=0; i<8; i++){
			chromo1_his[i] = chrom1[i+256];
			chromo2_his[i] = chrom2[i+256];
		}
		
		
		for(int i=0; i<1000; i++){
			//get the decisions of the chromosoms 
			choice1 = Data.getInstance().choice(chromo1_his, chrom1);
			choice2 = Data.getInstance().choice(chromo2_his, chrom2);
			
			//promote the history in a step
			shift(chromo1_his, chromo2_his);
			//update the last choices
			chromo1_his[6] = choice1;
			chromo1_his[7] = choice2;
			
			chromo2_his[6] = choice2;
			chromo2_his[7] = choice1;
			//update the score - fitness
			Data.getInstance().score(score, chromo1, chromo2, choice1, choice2);
			
		}
		
	}
	//end of the function play
	
	
	void shift(int arr1[], int arr2[]){
		for(int i=2; i<8; i++){
			arr1[i-2] = arr1[i];
			arr2[i-2] = arr2[i];
		}
	}
}
